﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Digitalmes.Model.AndonManagement;
using Digitalmes.Model;

namespace Digitalmes.ViewModel.AndonManagement.AndonStationVMs
{
    public partial class AndonStationListVM : BasePagedListVM<AndonStation_View, AndonStationSearcher>
    {
        
        protected override IEnumerable<IGridColumn<AndonStation_View>> InitGridHeader()
        {
            return new List<GridColumn<AndonStation_View>>{
                
                this.MakeGridHeader(x => x.AndonStation_Name).SetTitle(@Localizer["Page.名称"].Value),
                this.MakeGridHeader(x => x.AndonStation_Code).SetTitle(@Localizer["Page.编号"].Value),
                this.MakeGridHeader(x => x.AndonStation_DeviceUser).SetTitle(@Localizer["Page.设备名称"].Value),
                this.MakeGridHeader(x => x.AndonStation_EquipmentId).SetTitle(@Localizer["Page.关联设备"].Value),
                this.MakeGridHeader(x => x.AndonStation_TagId).SetTitle(@Localizer["Page.关联变量"].Value),
                this.MakeGridHeader(x => x.AndonStation_IpAddress).SetTitle(@Localizer["Page.主机地址"].Value),
                this.MakeGridHeader(x => x.AndonStation_TagAddress).SetTitle(@Localizer["Page.变量地址"].Value),
                this.MakeGridHeader(x => x.AndonStation_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.AndonStation_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.AndonStation_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.AndonStation_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.AndonStation_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<AndonStation_View> GetSearchQuery()
        {
            var query = DC.Set<AndonStation>()
                
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Code, x=>x.Code)
                .CheckEqual(Searcher.DeviceUserId, x=>x.DeviceUserId)
                .CheckEqual(Searcher.EquipmentId, x=>x.EquipmentId)
                .CheckEqual(Searcher.TagId, x=>x.TagId)
                .CheckContain(Searcher.IpAddress, x=>x.IpAddress)
                .CheckContain(Searcher.TagAddress, x=>x.TagAddress)
                .CheckContain(Searcher.Remark, x=>x.Remark)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x=>x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x=>x.UpdateBy)
                .Select(x => new AndonStation_View
                {
				    ID = x.ID,
                    
                    AndonStation_Name = x.Name,
                    AndonStation_Code = x.Code,
                    AndonStation_DeviceUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.DeviceUserId).Select(y => y.Name).FirstOrDefault(),
                    AndonStation_EquipmentId = x.EquipmentId,
                    AndonStation_TagId = x.TagId,
                    AndonStation_IpAddress = x.IpAddress,
                    AndonStation_TagAddress = x.TagAddress,
                    AndonStation_Remark = x.Remark,
                    AndonStation_CreateTime = x.CreateTime,
                    AndonStation_UpdateTime = x.UpdateTime,
                    AndonStation_CreateBy = x.CreateBy,
                    AndonStation_UpdateBy = x.UpdateBy,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    }
    public class AndonStation_View: AndonStation
    {
        
        public string AndonStation_Name { get; set; }
        public string AndonStation_Code { get; set; }
        public string AndonStation_DeviceUser { get; set; }
        public long? AndonStation_EquipmentId { get; set; }
        public long? AndonStation_TagId { get; set; }
        public string AndonStation_IpAddress { get; set; }
        public string AndonStation_TagAddress { get; set; }
        public string AndonStation_Remark { get; set; }
        public DateTime? AndonStation_CreateTime { get; set; }
        public DateTime? AndonStation_UpdateTime { get; set; }
        public string AndonStation_CreateBy { get; set; }
        public string AndonStation_UpdateBy { get; set; }

    }

}